package Game.GameElements 
{
	import Box2D.Collision.Shapes.b2MassData;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	import Common.ContactListener;
	import Common.Managers.SwfManager;
	import Common.Utils;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import Game.GameStates.GameState;
	import Game.Windows.DebugWindow;
	import Game.World;
	/**
	 * ...
	 * @author Yevgeniy Logachev
	 */
	public class Wall extends GameObject
	{
		public  static var 	m_swfPath:String 		= Settings.pathRes + "swf/impassable.swf";
		public 	static var 	m_wallTypes:Array 		= ["wall1", "wall2", "wall3", "wall4", "wall5", "wall6", "wall7", "wall8", "wall9", "back1", "decor1", "decor2"];
		
		private var 		m_damperKoef:Array		= [2.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0, 2.0, 2.0];
		private var 		m_massKoef:Array		= [1.0, 2.0, 3.1, 4.0, 1.0, 2.0, 3.1, 4.0, 1.3];
		private var 		m_isDestroyed:Boolean	= false;
				
		public function Wall(startX:int, startY:int, startRot:int, type:int, isPhysicalObject:Boolean = true, isStatic:Boolean = false) 
		{
			super(startX, startY, startRot, m_swfPath, m_wallTypes, type, isPhysicalObject, isStatic);
			World.instance.foregroundLayer.addChild(this);
			
			gameObjects.push(this);
		}
		
		public static function deserialize(data:ByteArray):Wall
		{
			var startX:int = data.readInt();
			var startY:int = data.readInt();
			var startRot:int = data.readInt();
			var type:int = data.readInt();
			var isPhisical:Boolean = data.readBoolean();
			var isStatic:uint = data.readUnsignedInt();
									
			return new Wall(startX, startY, startRot, type, isPhisical, (isStatic > 0) ? false : true);
		}
		
		override public function serialize(data:ByteArray):void
		{
			data.writeInt(x);
			data.writeInt(y);
			data.writeInt(rotation);
			data.writeInt(m_indxObj);
			data.writeBoolean((physicalObject != null) ? true : false);
			data.writeUnsignedInt((physicalObject != null) ? physicalObject.GetType() : 0);
		}
		
		public static function loadResource(funcHandler:Function):void
		{
			SwfManager.instance.load(m_swfPath, funcHandler);
		}
		
		public static function isLoaded():Boolean
		{
			return SwfManager.instance.isLoaded(m_swfPath);
		}
		
		override public function update():void
		{
			super.update();
			if (m_isDestroyed)
			{
				release();
			}
		}
		
		public function get isDestroy():Boolean
		{
			return m_isDestroyed;
		}
		
		override public function doDestroy():void
		{
			if ((physicalObject.GetType() != b2Body.b2_staticBody))
			{
				if (objectSprite.currentFrame == objectSprite.totalFrames)
				{
					m_isDestroyed = true;
				}
				else
				{
					objectSprite.nextFrame();
				}
			}
		}
	}

}